package com.masterclient.app.modules.bridge.presenter;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.eoner.baselib.presenter.ThrowableConsumer;
import com.eoner.commonbean.popup.CommonPopUpBean;
import com.eoner.baselib.container.CommonBaseResponse;
import com.masterclient.app.httpservice.RetrofitUtil2;
import com.masterclient.app.modules.bridge.BackGuideBean;
import com.masterclient.app.modules.bridge.TaskRetainBean;
import com.masterclient.app.modules.bridge.contract.BridgeContract;
import java.util.List;
import io.reactivex.functions.Consumer;
import okhttp3.RequestBody;

/**
 * @auther hjian
 * @date 2020/7/16.
 */
public class BridgePresenter extends BridgeContract.Presenter {

    @Override
    public void getPopUpData(String page_type, String trade_no, String order_type) {
        JSONObject popUpParam = new JSONObject();
        popUpParam.put("location",order_type);
        popUpParam.put("trade_no",trade_no);
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("scene",page_type);
            jsonObject.put("scene_value",popUpParam);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());

        mRxManager.add(toSubscribe(RetrofitUtil2.getInstance().commonDialogData(body), (Consumer<CommonBaseResponse<List<CommonPopUpBean>>>) dialogBean -> {
                            if (dialogBean.getCode() == 0){
                                mView.showData(dialogBean.getData());
                            } else {
                                mView.showFailure(dialogBean.getMsg());
                            }
                        },new ThrowableConsumer(){
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.showFailure("网络异常，请重试");
            }
        }));
    }

    @Override
    public void getBackDialogData() {
        mRxManager.add(toSubscribe(RetrofitUtil2.getInstance().getBackDialogData(), (Consumer<BackGuideBean>) dialogBean -> {
            if ("0".equals(dialogBean.getCode())){
                mView.showBackDialogData(dialogBean);
            }else {
                mView.showBackDialogDataFailure(dialogBean.getMsg());
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.showBackDialogDataFailure("网络异常，请重试");
            }
        }));
    }

    @Override
    public void getTaskBackDialog() {
        mRxManager.add(toSubscribe(RetrofitUtil2.getInstance().getTaskBackDialogData(), (Consumer<TaskRetainBean>) dialogBean -> {
            if ("0".equals(dialogBean.getCode())){
                mView.showTaskBackDialogData(dialogBean);
            }else {
                mView.showTaskBackDialogDataFailure(dialogBean.getMsg());
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.showTaskBackDialogDataFailure("网络异常，请重试");
            }
        }));
    }
}
